package com.dtagri.fjwlw.base;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;

import com.dtagri.fjwlw.AppContext;
import com.dtagri.fjwlw.util.TPreferences;

/**
 * 上下文基类
 * 
 * @author wxjn
 * 
 */
public class BaseApplication extends Application {
	public static final String KEY_LAST_CODE = "KEY_CUR_CODE";

	static Context _context;
	private static boolean sIsAtLeastGB;

	static {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			sIsAtLeastGB = true;
		}
	}

	@Override
	public void onCreate() {
		super.onCreate();
		_context = getApplicationContext();
	}

	public static synchronized AppContext context() {
		return (AppContext) _context;
	}

	@TargetApi(Build.VERSION_CODES.GINGERBREAD)
	protected static void apply(SharedPreferences.Editor editor) {
		if (sIsAtLeastGB) {
			editor.apply();
		} else {
			editor.commit();
		}
	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	protected static SharedPreferences getPreferences(String prefName) {
		return context().getSharedPreferences(prefName,
				Context.MODE_MULTI_PROCESS);
	}

	public static int getLastCode() {
		return TPreferences.get(KEY_LAST_CODE, 1);
	}

	public static void setLastCode(int value) {
		TPreferences.set(KEY_LAST_CODE, value);
	}
}
